package com.ai;

import cn.hutool.json.JSONUtil;
import com.ai.bean.BeanMangerOnly;
import com.ai.business.MyKeywordStudy;
import com.ai.business.MySentence;
import com.ai.config.Config;
import com.ai.mydata.entity.KeywordSql;
import com.ai.mydata.entity.KeywordType;
import com.ai.mydata.entity.MyTree;
import com.ai.mydata.entity.Sentence;
import com.ai.mydata.mapper.SqlMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.wlld.config.SentenceConfig;
import org.wlld.entity.SentenceModel;
import org.wlld.entity.WordTwoVectorModel;
import org.wlld.naturalLanguage.word.WordEmbedding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author PC
 * @version 1.0
 * @description: TODO
 * @date 2024/11/11 15:06
 */
@SpringBootApplication
@MapperScan("com.ai.mydata.mapper")
public class EasyAiApplication {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(EasyAiApplication.class, args);
        //创建websocket实例
        info(applicationContext);
    }


    public static void info (ConfigurableApplicationContext applicationContext) throws Exception{
        BeanMangerOnly beanMangerOnly = applicationContext.getBean(BeanMangerOnly.class);
        WordEmbedding wordEmbedding = beanMangerOnly.getWordEmbedding();
        SqlMapper sql = applicationContext.getBean(SqlMapper.class);
        List<MySentence> sentences = sql.getModel2();
        SentenceModel sentenceModel = new SentenceModel();
        for (MySentence sentence : sentences) {
            sentenceModel.setSentence(sentence.getWord());//语句模板实体类塞入语句样本
        }
        wordEmbedding.init(sentenceModel, beanMangerOnly.getConfig().getWordVectorDimension());
        WordTwoVectorModel wordTwoVectorModel = wordEmbedding.start();//词向量开始学习
        String model = JSONUtil.toJsonStr(wordTwoVectorModel);
        System.out.println(model);
        System.out.println("完成初始化");
    }

    /*public static void init(ConfigurableApplicationContext applicationContext) throws Exception {//初始化启动配置

        List<MySentence> sentences = new ArrayList<>();
        //配置文件
        BeanMangerOnly beanMangerOnly = applicationContext.getBean(BeanMangerOnly.class);
        Map<Integer, MySentence> sentenceMap = new HashMap<>();

        SqlMapper sql = applicationContext.getBean(SqlMapper.class);
        List<Sentence> sentencesList = sql.getModel();
        List<KeywordSql> keywordSqlList = sql.getKeywordSql();
        //配置文件
        SentenceConfig sentenceConfig = beanMangerOnly.getConfig();

        List<MyTree> trees = sql.getMyTree();
        sentenceConfig.setTypeNub(trees.size());
        beanMangerOnly.getWordEmbedding().setConfig(sentenceConfig);
        beanMangerOnly.getRRNerveManager().init(sentenceConfig);

        for (Sentence sentence : sentencesList) {
            MySentence mySentence = new MySentence();
            mySentence.setTypeId(sentence.getTypeId());
            mySentence.setWord(sentence.getWord());
            sentences.add(mySentence);
            sentenceMap.put(sentence.getSentenceId(), mySentence);
        }
        for (KeywordSql keywordSql : keywordSqlList) {
            MyKeywordStudy myKeywordStudy = new MyKeywordStudy();
            myKeywordStudy.setKeyword(keywordSql.getKeyword());
            myKeywordStudy.setKeywordTypeId(keywordSql.getKeywordTypeId());
            int sentence_id = keywordSql.getSentenceId();
            if (sentenceMap.containsKey(sentence_id)) {
                sentenceMap.get(sentence_id).getMyKeywordStudyList().add(myKeywordStudy);
            } else {
                throw new Exception("关键词表 keyword_sql sentence_id:" + sentence_id + ",无法在sentence表找到对应的语句 sentence_id:" + sentence_id);
            }
        }
//        applicationContext.getBean(BeanManger.class).tools().initSemantics(beanMangerOnly, sentences, Config.selfTest);
        System.out.println("完成初始化");
    }*/
}
